package LeetCode_Stack;

import java.util.LinkedList;

/**
 * Created with IntelliJ IDEA.
 * Description:用队列实现栈
 * User: 阿宾
 * Date: 2024-11-14
 * Time: 15:29
 */
public class Demo5 {
    static class MyStack {

        LinkedList<Integer> qu1;
        LinkedList<Integer> qu2;
        LinkedList<Integer> queue;
        public MyStack() {
            this.qu1 = new LinkedList<>();
            this.qu2 = new LinkedList<>();
            this.queue = null;
        }

        //入栈
        public void push(int x) {
            if(!qu1.isEmpty()){
                qu1.offer(x);
            }else if(!qu2.isEmpty()){
                qu2.offer(x);
            }else {
                qu1.offer(x);
            }
        }

        //移除并返回栈顶元素
        public int pop() {
            if(empty()){
                return -1;
            }
            if(!qu1.isEmpty()){
                while (qu1.size()-1 != 0){
                    int tmp = qu1.poll();
                    qu2.offer(tmp);
                }
                return qu1.poll();
            }else {
                while (qu2.size()-1 != 0){
                    int tmp = qu2.poll();
                    qu1.offer(tmp);
                }
                return qu2.poll();
            }
        }

        //返回栈顶元素
        public int top() {
            if(empty()){
                return -1;
            }
            int tmp = 0;
            if(!qu1.isEmpty()){
                while (qu1.size() != 0){
                    tmp = qu1.poll();
                    qu2.offer(tmp);
                }
                return tmp;
            }else {
                while (qu2.size() != 0){
                    tmp = qu2.poll();
                    qu1.offer(tmp);
                }
                return tmp;
            }
        }

        //栈是否为空
        public boolean empty() {

            if(qu1.isEmpty() && qu2.isEmpty()){
                return true;
            }
            return false;
        }
    }
}
